package com.gwsdk.paysdk.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.gwsdk.open.ApiCallback;
import com.gwsdk.open.GPExitResult;
import com.gwsdk.paysdk.bean.InitModel;
import com.gwsdk.paysdk.listener.OnMultiClickListener;
import com.gwsdk.paysdk.utils.GWInflaterUtils;
import com.gwsdk.paysdk.utils.ScreenshotUtils;

public final class DialogUtil {
    public static Dialog mDialog;

    private DialogUtil() {
    }

    private static DialogUtil single = null;

    public static DialogUtil getInstance() {
        if (single == null) {
            single = new DialogUtil();
        }
        return single;
    }

    /**
     * This is the system exit pop tips
     *
     * @param context
     * @param pTitle
     * @param pMsg
     */
    public static void showCustomMessage(final Activity context, String pTitle, final String pMsg, String ok, String cancel) {
        final GPExitResult exitResult = new GPExitResult();
        final Dialog lDialog = new Dialog(context,getIdByName(context,"style","gw_select_ptb_type_dialog"));
        lDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        lDialog.setContentView(getIdByName(context,"layout","gw_dialog_alert_exit_main_light"));
        ((TextView) lDialog.findViewById(getIdByName(context,
                "id", "dialog_title"))).setText(pTitle);
        ((TextView) lDialog.findViewById(getIdByName(context,
                "id", "dialog_message"))).setText(pMsg);
        ((Button) lDialog.findViewById(getIdByName(context, "id",
                "ok"))).setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {  //点击了确认退出按钮
                lDialog.dismiss();
                exitResult.mResultCode = GPExitResult.GPSDKExitResultCodeExitGame;
                if (null != ApiCallback.getExitObsv()) {
                    InitModel.init().offLine(context, true);

                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ScreenshotUtils.getInstance().onDestroy(); //解除屏幕截图广播注册
                    ApiCallback.getExitObsv().onExitFinish(exitResult);
                }
                return;
            }
        });
        ((Button) lDialog.findViewById(getIdByName(context, "id",
                "ok"))).setText(ok);
        lDialog.show();
        ((Button) lDialog.findViewById(getIdByName(context, "id",
                "cancel"))).setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                lDialog.dismiss();
                exitResult.mResultCode = GPExitResult.GPSDKExitResultCodeCloseWindow;
                if (null != ApiCallback.getExitObsv()) {
                    ApiCallback.getExitObsv().onExitFinish(exitResult);
                }
                return;
            }
        });

        ((Button) lDialog.findViewById(getIdByName(context, "id",
                "cancel"))).setText(cancel);
    }


    public static Dialog lDialog;

    public static void gw_alert_exit(Activity context) {
        DialogUtil.showCustomMessage(context, "提示", "确定要退出吗？", "确定", "取消");
    }


    public static Dialog gw_alert_msg(final Context mypay, String pTitle,
                                      final String pMsg, final Context getApplicationContext, String substr,
                                      String clestr, final OnClickListener sublis) {
        final Dialog lDialog = new Dialog(mypay, GWInflaterUtils.getIdByName(mypay,"style","gw_select_ptb_type_dialog"));
        lDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        lDialog.setContentView(GWInflaterUtils.getLayout(mypay,"gw_dialog_alert_exit_main_light"));
        ((TextView) lDialog.findViewById(GWInflaterUtils.getControl(mypay,"dialog_title"))).setText(pTitle);
        ((TextView) lDialog.findViewById(GWInflaterUtils.getControl(mypay,"dialog_message"))).setText(pMsg);
        Button okButton = lDialog.findViewById(GWInflaterUtils.getControl(mypay,"ok"));
        Button cancelButton = lDialog.findViewById(GWInflaterUtils.getControl(mypay,"cancel"));
        okButton.setOnClickListener(new OnMultiClickListener() {

            @Override
            public void onMultiClick(View v) {
                sublis.onClick(v);
                lDialog.dismiss();
            }
        });
        okButton.setText(substr);
        cancelButton.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                lDialog.dismiss();
            }
        });

        cancelButton.setText(clestr);
        return lDialog;
    }



    public static Dialog gw_down_time_alert(final Context context, final String pMsg, String substr, final OnClickListener sublis) {
        final Dialog lDialog = new Dialog(context, GWInflaterUtils.getIdByName(context,"style","gw_select_ptb_type_dialog"));
        lDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        lDialog.setContentView(GWInflaterUtils.getLayout(context,"gw_dialog_alert_exit_main_light2"));
        ((TextView) lDialog.findViewById(GWInflaterUtils.getControl(context,"dialog_message"))).setText(pMsg);
        ((TextView) lDialog.findViewById(GWInflaterUtils.getControl(context,"dialog_message"))).setTextSize(14);
        Button okButton = lDialog.findViewById(GWInflaterUtils.getControl(context,"ok"));
        okButton.setOnClickListener(new OnMultiClickListener() {
                    @Override
                    public void onMultiClick(View v) {
                        sublis.onClick(v);
                        lDialog.dismiss();
                    }
        });
        okButton.setText(substr);
        return lDialog;
    }



    /**
     * Refer to external project resources
     *
     * @param context
     * @param className
     * @param name
     * @return
     */
    private static int getIdByName(Context context, String className, String name) {
        if (null == context) {
            return -1;
        }
        int id = getIdByContext(context, className, name);
        if (id > 0) {
            return id;
        }
        String packageName = context.getPackageName();
        Class r = null;
        id = 0;
        try {
            r = Class.forName(packageName + ".R");
            Class[] classes = r.getClasses();
            Class desireClass = null;
            for (int i = 0; i < classes.length; ++i) {
                if (classes[i].getName().split("\\$")[1].equals(className)) {
                    desireClass = classes[i];
                    break;
                }
            }
            if (desireClass != null)
                id = desireClass.getField(name).getInt(desireClass);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return id;
    }

    private static int getIdByContext(Context context, String className,
                                      String name) {
        Resources res = null;
        int id = 0;
        try {
            res = context.getResources();
            id = res.getIdentifier(name, className, context.getPackageName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return id;
    }


}